package com.example.controller;


import com.example.pojo.Department;
import com.example.pojo.PageBean;
import com.example.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-08-20
 */
@Controller
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;

    /**
     * 展示页面
     * @return
     */
    @RequestMapping("/list.do")
    public String getDepartmentList(Model model,Integer parentId,Integer currentPage,Integer pageSize) {
        if (currentPage == null)
            currentPage = 1;

        if (pageSize == null)
            pageSize = 5;

        PageBean<Department> pageBean = departmentService.getPage(parentId, currentPage, pageSize);
        model.addAttribute("departmentList", pageBean.getRecordList());
        model.addAttribute("pageBean", pageBean);

        //返回上一级所需ID
        Department department = departmentService.getById(parentId);
        if (department != null) {
            model.addAttribute("parentId", department.getParentId());
        } else {
            model.addAttribute("parentId", null);
        }
        //如果有父部门，显示子部门分页
        if (parentId != null) {
            model.addAttribute("url", "/department/list.do?parentId=" + parentId);
        } else {
            model.addAttribute("url", "/department/list.do?");
        }
        //用于添加查询父部门Id
        model.addAttribute("deptId", parentId);

        return "/jsp/department/list.jsp";
    }

    /**
     * 返回上一级展示页面
     */
    @RequestMapping("/listTop.do")
    public String  goBackTo(Model model,Integer parentId,Integer currentPage,Integer pageSize) {
        if (currentPage == null)
            currentPage = 1;
        if (pageSize == null)
            pageSize = 5;
        PageBean<Department> pageBean = departmentService.getPage(parentId, currentPage, pageSize);
        model.addAttribute("departmentList", pageBean.getRecordList());
        model.addAttribute("pageBean", pageBean);

        //返回上一级所需ID
        Department department = departmentService.getById(parentId);
        if (department != null) {
            model.addAttribute("parentId", department.getParentId());
        } else {
            model.addAttribute("parentId", null);
        }
        //如果有父部门，显示子部门分页
        if (parentId != null) {
            model.addAttribute("url", "/department/list.do?parentId=" + parentId);
        } else {
            model.addAttribute("url", "/department/list.do?");
        }
        //用于添加查询父部门Id
        model.addAttribute("deptId", parentId);
        return "/jsp/department/list.jsp";
    }

    /**
     * 跳转添加部门页面
     * @param parentId
     * @return
     */
    @RequestMapping("/addUI.do")
    public String addDepartment(Integer parentId, Model model) {
        //查询部门列表
        List<Department> deptList = departmentService.getDeptTree(null);
        model.addAttribute("deptList", deptList);

        //放置父部门信息
        if (parentId!=null) {
            //只放置id
            Department department = new Department();
            department.setParentId(parentId);
            model.addAttribute("department",department);
        }

        return "/jsp/department/save.jsp";
    }

    /**
     * 跳转到修改页面
     * @return
     */
    @RequestMapping("/editUI.do")
    public String editDepartmentUI(Integer id, Model model) {
        Department department = departmentService.getById(id);
        model.addAttribute("department", department);

        //查询部门列表
        List<Department> deptList = departmentService.getParentTree(id);

        model.addAttribute("deptList", deptList);
        return "/jsp/department/save.jsp";
    }

    /**
     * 修改部门
     * @return
     */
    @RequestMapping("/edit.do")
    public String editDepartment(Integer depid,Integer parentId,String name,String description) {
        Department department = new Department();
        department.setDepid(depid);
        department.setParentId(parentId);
        department.setName(name);
        department.setDescription(description);

        departmentService.updateById(department);

        return "/department/list.do";
    }

    /**
     * 添加部门信息
     * @param parentId
     * @param name
     * @param description
     * @return
     */
    @RequestMapping("/add.do")
    public String addDeptDo(Integer parentId,String name,String description) {
        Department department = new Department();
        department.setParentId(parentId);
        department.setName(name);
        department.setDescription(description);

        departmentService.save(department);

        return "/department/list.do";
    }

    /**
     * 删除部门及其子部门
     * @param id
     * @return
     */
    @RequestMapping("/delete.do")
    public String deleteDeptAndSon(Integer id) {
        departmentService.deleteDeptAndSon(id);

        return "/department/list.do";
    }

}

